package com.controller;

import com.bean.User;
import com.service.UserService;
import com.service.impl.UserServiceImpl;
import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.FileItemFactory;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.IOException;
import java.util.List;

public class UserModifyPhoto extends HttpServlet {
    private UserService userService=new UserServiceImpl();

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("UTF-8");
        resp.setContentType("text/html;charset=UTF-8");
        System.out.println("过来了！");
        User user= (User) req.getSession().getAttribute("user");
        String userid=user.getUserid();

        //1、判断上传的数据是否多段数据
        if(ServletFileUpload.isMultipartContent(req)){
            //创建 FileItemFactory 工厂实现类
            FileItemFactory fileItemFactory=new DiskFileItemFactory();
            //创建用于解析上传数据的工具类 ServletFileUpload
            ServletFileUpload servletFileUpload=new ServletFileUpload(fileItemFactory);
            //解析上传数据，得到每一个表单项fileItem
            try {
                List<FileItem> list=servletFileUpload.parseRequest(req);
                String head_sculpture="";
                //循环判断，每一和表单项，是普通表单项，还是上传的文件
                for(FileItem fileItem:list){
                    if(fileItem.isFormField()){
                        //普通表单项
                        System.out.println("name属性值"+fileItem.getFieldName());
                        userid=fileItem.getString();
                        System.out.println("value属性值"+fileItem.getString("UTF-8"));
                    }else{
                        //上传的文件
                        System.out.println("name属性值"+fileItem.getFieldName());
                       /*  head_sculpture=fileItem.getName();*/
                        head_sculpture=userid+".jpg";
                         System.out.println("文件名"+ head_sculpture);
                        System.out.println("文件名"+fileItem.getName());
                       String imgpath="D:\\IDEAProgram\\onlineflower\\web\\static\\picUser\\"+head_sculpture;
                      File file=new File(imgpath);
                      if(file.exists()){
                          file.delete();
                          System.out.println(head_sculpture+"原来头像已经删除！");
                      }
                        fileItem.write(new File("D:\\IDEAProgram\\onlineflower\\web\\static\\picUser\\"+head_sculpture));

                       /* fileItem.write(new File("D:\\IDEAProgram\\onlineflower\\web\\static\\picUser\\"+fileItem.getName()));*/
                    }
                }
                userService.modifyPhoto(userid,head_sculpture);
                user.setHead_sculpture(head_sculpture);
                //req.getSession().removeAttribute("user");
                //System.out.println(user);
                user=userService.showMess(userid);
                req.getSession().setAttribute("user",user);
               System.out.println(user);
                req.getRequestDispatcher("/page/user/showMess.jsp").forward(req,resp);

            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doPost(req,resp);
    }
}
